
<template>
<el-container class="home_container">
    <!-- 头部区域 -->
  <el-header>
    <span class="title">电商管理后台</span>
    <el-button type="info" @click="logout">退出</el-button>
  </el-header>
    <el-container>
        <el-aside :width="iscollapse ? '64px' : '200px'">
            <!-- 折叠效果 -->
            <div class="toggle_button" @click="toggleCollapse">|||</div>
            <el-menu
            :router="true"
            :collapse="iscollapse"
            :default-active="activePath"
            :collapse-transition="false"
            unique-opened
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409FE0">
      <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id" >
        <template slot="title">
          <i :class="icons[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <el-menu-item :index="'/'+sitem.path" v-for="sitem in item.children" :key="sitem.id" @click="saveLink('/'+sitem.path)">
             <template slot="title">
          <i class="el-icon-menu"></i>
          <span>{{sitem.authName}}</span>
        </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>

        </el-aside>
        <el-main>
            <router-view/>
        </el-main>
   </el-container>
</el-container>
</template>
<script>

export default {
  name: 'home',
  data(){
      return{
        menulist:[],
        icons:{
            '125':'el-icon-s-custom',
            '103':'el-icon-s-claim',
            '101':'el-icon-s-shop',
            '102':'el-icon-s-order',
            '145':'el-icon-s-operation',
        },
        // 是否折叠
        iscollapse:false,
        //被激活的链接地址
        activePath:''
      }
  },
  created(){
      this.getMenuList();
      this.activePath=window.sessionStorage.getItem('activePath')
  },
  methods:{
      logout(){
          window.sessionStorage.clear()
          this.$router.push('/login')
      },
      async getMenuList(){
          const{data:res} =await this.$http.get('menus');
          if(res.meta.status!=200){
              this.$message.error(res.meta.msg)
          }else{
              this.menulist=res.data
              this.icons.id
          }
      },
    //   折叠和收起菜单
      toggleCollapse(){
          this.iscollapse=!this.iscollapse
      },
    //   保存访问的链接地址
    saveLink(activePath){
        window.sessionStorage.setItem('activePath',activePath)
        this.activePath=window.sessionStorage.getItem('activePath')
    }
  }
}
</script>

<style scoped>
    .el-header{
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .logo img{
        width: 100px;
        height: 100px;
    }
    .title{
        font-size: 26px;
        color: #fff;
    }
    .el-aside{
        background-color: #333744;
    }
    .toggle_button{
        background-color: #4a5064;
        font-size: 12px;
        color: #fff;
        line-height: 24px;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
    }
    .el-menu{
        border-right: none;
    }
    .el-main{
        background-color: #eaedf1;
    }
    .home_container{
        width: 100%;
        height: 100%;
    }
</style>
